#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tornado.web import RequestHandler
import pymongo
import re
import render
import codecs
from _base import BaseHandler
import datetime
import sys
sys.path.insert(0,'/home/leeray/workspaces/Python/wanalytics/')
sys.path.insert(0,'/opt/web/')
from Tools import getPid
from Tools import connectMySQL 
import json
from util import cache, settings
import _clientCache as clientcache
import _clientUser as clientuser
import logging
import time
import tornado.ioloop


class MainClient(BaseHandler):
    def get(self): 
        os_json = clientcache.getOS()
        
        pidtype_json = clientcache.getPidType()
        
        _time = self.get_argument('staDate', '2011/11/13').strip()
        _pd = self.get_argument('pd','').strip()
        try:
            st_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
        except :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        if not _pd:
            ed_time = datetime.datetime.now()
            delta = ed_time - st_time 
            _pd =  abs(delta.days)
        
        _pd = int(_pd)
        et_time = st_time + datetime.timedelta(days=_pd)
        st_datestr = st_time.strftime('%Y%m%d')
        et_datestr = et_time.strftime('%Y%m%d')
        
        all_data = []
        results = {}
        os_data = []
        
        #connection = pymongo.Connection("10.103.13.42", 27017)
        #db = connection.DailyLog
        db = connectMySQL.connMySQL_stat()
        c=db.cursor()
        #sql = "SELECT date, os, type, pv, uv, vv  FROM `view_day_mainAnalytics` WHERE date>= '{0}' and date < '{1}'".format(st_datestr, et_datestr)
        #sql = "SELECT date, os_id, pid_type, pv, uv, vv  FROM `viewdaymain` WHERE date>= '{0}' and date < '{1}'".format(st_datestr, et_datestr)
        sql = "SELECT date, os_id, device_type, sum(pv), sum(uv), sum(vv),sum(newuser)  FROM `stat_day_total` WHERE date>= '{0}' and date < '{1}' and ( business_type=2 or business_type=1) and pid_stat=1 group by date, os_id, device_type".format(st_datestr, et_datestr)
        logging.info(sql)
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        for x in res:
            date = str(x[0])
            os = os_json.get(int(x[1])) or 'null'
            #type1 = pidtype_json.get(int(x[2])) or 'null'
            if int(x[1]) != 72:
                type1 = settings.device_type.get(str(x[2])) or 'unkown'
            else :
                type1 = ''
            pv = int(x[3])
            uv = int(x[4])
            vv = int(x[5])
            nu = int(x[6])
            
            if _tmpdate and _tmpdate != date:
                results['data'] = os_data
                results['year'] = _tmpdate[:4]
                results['month'] = _tmpdate[4:6]
                results['day'] = _tmpdate[6:]
                all_data.append(results)
                results = {}
                os_data = []
            
            os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nu': nu})
            _tmpdate = date
        
        c.close()
        db.close()
        
        if len(os_data) > 0 :
            results['data'] = os_data
            results['year'] = _tmpdate[:4]
            results['month'] = _tmpdate[4:6]
            results['day'] = _tmpdate[6:]
            all_data.append(results)
            results = {}
            os_data = []

        self.write({"status":"success", "items":all_data})
        self.finish()

class MainTotalUser(BaseHandler):
    def get(self): 
        os_json = clientcache.getOS()
        pidtype_json = clientcache.getPidType()
        
        _time = self.get_argument('endDate', '').strip()
        try:
            ed_datestr = None
            if _time :
                ed_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
                ed_datestr = ed_time.strftime('%Y%m%d')
            else :
                ed_time = None
        except :
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            self.send_error(400, desc='invalid argument')
            return
        
        
        
        os_data = []

        db = connectMySQL.connMySQL_stat()
        c=db.cursor()
        #sql = "SELECT date, os_id, device_type, alluser  FROM `stat_day_main` group by os_id order by alluser desc"
        if not ed_datestr :
            sql = '''select tmp_a.date, tmp_a.os_id, tmp_a.device_type, max(tmp_a.alluser)  from 
            (SELECT date, os_id, device_type, sum(alluser) alluser FROM `stat_day_total` where date<=(select max(b.date) from `stat_day_total` b) and business_type=2 and pid_stat=1 group by date, os_id, device_type order by date desc) as tmp_a 
            group by tmp_a.os_id, tmp_a.device_type'''
            #sql = "SELECT date, os_id, device_type, sum(alluser)  FROM `stat_day_main` where date = (select max(b.date) from `stat_day_main` b) and ( business_type=2 or business_type=1)  and partner_mode=3 group by date, os_id, device_type"
        else :
            sql = '''select tmp_a.date, tmp_a.os_id, tmp_a.device_type, max(tmp_a.alluser)  from 
            (SELECT date, os_id, device_type, sum(alluser) alluser FROM `stat_day_total` where date<={0}  and business_type=2 and pid_stat=1 group by date, os_id, device_type order by date desc) as tmp_a 
            group by tmp_a.os_id, tmp_a.device_type'''.format(ed_datestr)
            #sql = "SELECT distinct date, os_id, device_type, sum(alluser)  FROM `stat_day_main` where date <= {0} and ( business_type=2 or business_type=1)  and partner_mode=3 group by date, os_id, device_type".format(ed_datestr)
            
        logging.info(sql)
        
        c.execute(sql)  
        res= c.fetchall()
        for x in res:
            os = os_json.get(int(x[1])) or 'null'
            #type1 = pidtype_json.get(int(x[2])) or 'null'
            type1 = settings.device_type.get(str(x[2])) or 'unkown'
            tu = int(x[3])
            os_data.insert(0,{'os':os, 'type':type1, 'tu':tu})

        c.close()
        db.close()
        
        self.write({"status":"success", "items":os_data})
        self.finish()
        
class PromoFactoryName(BaseHandler):
    def get(self): 
        resjson = clientcache.getPromoFactoryName()
        
        self.write(resjson)
        self.finish()
 
class PromoAnalytics(BaseHandler):
    def get(self): 
        _uname = self.get_argument('uname', '').strip() or ''
        _pwd = self.get_argument('pwd','').strip() or ''
        
        cu = clientuser.UserCheck()
        userlogin = cu.check(_uname, _pwd)
        
        if not userlogin or userlogin.get('status', 'failed') != 'success' :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        _fid = self.get_argument('fid', '0').strip()
        if userlogin.get('results') and userlogin.get('results').get('type','2') == 2 and int(_fid) != int(userlogin.get('results').get('userid', '0')):
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        _time = self.get_argument('staDate', '2011/11/13').strip()
        _pd = self.get_argument('pd','').strip()
        
        try:
            st_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
        except :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        if not _pd:
            ed_time = datetime.datetime.now()
            delta = ed_time - st_time 
            _pd =  abs(delta.days)
            
        _pd = int(_pd)
        et_time = st_time + datetime.timedelta(days=_pd)
        st_datestr = st_time.strftime('%Y%m%d')
        et_datestr = et_time.strftime('%Y%m%d')
        
        os_json = clientcache.getOS()
        pidtype_json = clientcache.getPidType()
        factory_json = clientcache.getFactoryName()
        pid_json = clientcache.getParnter()
        
        all_data = []
        results = {}
        os_data = []
        
        db = connectMySQL.connMySQL_stat()
        c=db.cursor()        
        sql = "SELECT date, f_id, pid, pv, uv, vv, newuser  FROM `stat_day_channel` WHERE date>= '{0}' and date < '{1}' and f_id={2} and partner_mode=2 and pid_stat=1 ORDER BY date".format(st_datestr, et_datestr, _fid)
        logging.info(sql)
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        for x in res:
            date = str(x[0])
            
            #pname = factory_json.get(x[1],'')
            pid = x[2]
            pname = pid_json.get(pid,['',''])[0]            
            pv = x[3]
            uv = x[4]
            vv = x[5]
            nu = x[6]
            
            if _tmpdate and _tmpdate != date:
                results['data'] = os_data
                results['year'] = _tmpdate[:4]
                results['month'] = _tmpdate[4:6]
                results['day'] = _tmpdate[6:]
                all_data.append(results)
                results = {}
                os_data = []
            
            os_data.insert(0,{'pname':pname, 'pid':pid, 'pv':pv, 'uv':uv, 'vv':vv, 'nu': nu})
            _tmpdate = date
        
        c.close()
        db.close()
          
        if len(os_data) > 0 :
            results['data'] = os_data
            results['year'] = _tmpdate[:4]
            results['month'] = _tmpdate[4:6]
            results['day'] = _tmpdate[6:]
            all_data.append(results)
            results = {}
            os_data = []
        
        #test = {"items": [{"year": "2011", "data": [{"pv": 8706151, "vv": 883726, "pid": "749c360516ebe533", "uv": 379018, "pname": "mobile优酷友情合作", "nu": 883726}, {"pv": 23036219, "vv": 2646085, "pid": "749c360516ebe533", "uv": 618283, "pname": "3G门户G浏览器", "nu": 2646085}, {"pv": 20426465, "vv": 10197784, "pid": "749c360516ebe533", "uv": 177077, "pname": "百信手机网外链", "nu": 10197784}, {"pv": 5984380, "vv": 675899.0, "pid": "749c360516ebe533", "uv": 380541, "pname": "航海桌面导航", "nu": 675899.0}], "day": "20", "month": "11"}, {"year": "2011", "data": [{"pv": 7738457, "vv": 788236, "pid": "749c360516ebe533", "uv": 344861, "pname": "mobile优酷友情合作", "nu": 788236}, {"pv": 19551225, "vv": 2101659, "pid": "749c360516ebe533", "uv": 510932, "pname": "3G门户G浏览器", "nu": 2101659}, {"pv": 18427618, "vv": 9257357, "pid": "749c360516ebe533", "uv": 157920, "pname": "百信手机网外链", "nu": 9257357}, {"pv": 5501749, "vv": 607562.0, "pid": "749c360516ebe533", "uv": 346352, "pname": "航海桌面导航", "nu": 607562.0}], "day": "21", "month": "11"}, {"year": "2011", "data": [{"pv": 7954177, "vv": 815959, "pid": "749c360516ebe533", "uv": 349300, "pname": "mobile优酷友情合作", "nu": 815959}, {"pv": 17299241, "vv": 2118076, "pid": "749c360516ebe533", "uv": 501143, "pname": "3G门户G浏览器", "nu": 2118076}, {"pv": 20366128, "vv": 10192610, "pid": "749c360516ebe533", "uv": 162041, "pname": "百信手机网外链", "nu": 10192610}, {"pv": 6219346, "vv": 899602, "pid": "749c360516ebe533", "uv": 363954, "pname": "航海桌面导航", "nu": 899602}], "day": "22", "month": "11"}, {"year": "2011", "data": [{"pv": 7977166, "vv": 842794, "pid": "749c360516ebe533", "uv": 348040, "pname": "mobile优酷友情合作", "nu": 842794}, {"pv": 16584716, "vv": 2097957, "pid": "749c360516ebe533", "uv": 490854, "pname": "3G门户G浏览器", "nu": 2097957}, {"pv": 20198366, "vv": 10391665, "pid": "749c360516ebe533", "uv": 161251, "pname": "百信手机网外链", "nu": 10391665}, {"pv": 5815811, "vv": 696805, "pid": "749c360516ebe533", "uv": 364588, "pname": "航海桌面导航", "nu": 696805}], "day": "23", "month": "11"}, {"year": "2011", "data": [{"pv": 7903879, "vv": 847485, "pid": "749c360516ebe533", "uv": 346739, "pname": "mobile优酷友情合作", "nu": 847485}, {"pv": 15089805, "vv": 2072196, "pid": "749c360516ebe533", "uv": 486514, "pname": "3G门户G浏览器", "nu": 2072196}, {"pv": 21005978, "vv": 10992027, "pid": "749c360516ebe533", "uv": 160747, "pname": "百信手机网外链", "nu": 10992027}, {"pv": 5907725, "vv": 714918, "pid": "749c360516ebe533", "uv": 365872, "pname": "航海桌面导航", "nu": 714918}], "day": "24", "month": "11"}, {"year": "2011", "data": [{"pv": 8036462, "vv": 859703, "pid": "749c360516ebe533", "uv": 351217, "pname": "mobile优酷友情合作", "nu": 859703}, {"pv": 16574256, "vv": 2147187, "pid": "749c360516ebe533", "uv": 508284, "pname": "3G门户G浏览器", "nu": 2147187}, {"pv": 20814242, "vv": 10802160, "pid": "749c360516ebe533", "uv": 166903, "pname": "百信手机网外链", "nu": 10802160}, {"pv": 5869896, "vv": 742478, "pid": "749c360516ebe533", "uv": 370612, "pname": "航海桌面导航", "nu": 742478}], "day": "25", "month": "11"}, {"year": "2011", "data": [{"pv": 9069765, "vv": 972571, "pid": "749c360516ebe533", "uv": 387447, "pname": "mobile优酷友情合作", "nu": 972571}, {"pv": 19110457, "vv": 2686480, "pid": "749c360516ebe533", "uv": 607180, "pname": "3G门户G浏览器", "nu": 2686480}, {"pv": 22990850, "vv": 12016920, "pid": "749c360516ebe533", "uv": 189509, "pname": "百信手机网外链", "nu": 12016920}, {"pv": 6409802, "vv": 819128, "pid": "749c360516ebe533", "uv": 408271, "pname": "航海桌面导航", "nu": 819128}], "day": "26", "month": "11"}, {"year": "2011", "data": [{"pv": 5164029, "vv": 905470, "pid": "749c360516ebe533", "uv": 402251, "pname": "mobile优酷友情合作", "nu": 905470}, {"pv": 22663416, "vv": 2959138, "pid": "749c360516ebe533", "uv": 625509, "pname": "3G门户G浏览器", "nu": 2959138}, {"pv": 22769949, "vv": 12005075, "pid": "749c360516ebe533", "uv": 194499, "pname": "百信手机网外链", "nu": 12005075}, {"pv": 6496387, "vv": 824110, "pid": "749c360516ebe533", "uv": 405406, "pname": "航海桌面导航", "nu": 824110}], "day": "27", "month": "11"}, {"year": "2011", "data": [{"pv": 7883670, "vv": 923891, "pid": "749c360516ebe533", "uv": 364508, "pname": "mobile优酷友情合作", "nu": 923891}, {"pv": 16813831, "vv": 2227148, "pid": "749c360516ebe533", "uv": 523621, "pname": "3G门户G浏览器", "nu": 2227148}, {"pv": 21876510, "vv": 11389919, "pid": "749c360516ebe533", "uv": 177008, "pname": "百信手机网外链", "nu": 11389919}, {"pv": 6182231, "vv": 786666, "pid": "749c360516ebe533", "uv": 377465, "pname": "航海桌面导航", "nu": 786666}], "day": "28", "month": "11"}, {"year": "2011", "data": [{"pv": 8932697, "vv": 979964, "pid": "749c360516ebe533", "uv": 371687, "pname": "mobile优酷友情合作", "nu": 979964}, {"pv": 18295828, "vv": 2271816, "pid": "749c360516ebe533", "uv": 529236, "pname": "3G门户G浏览器", "nu": 2271816}, {"pv": 24524788, "vv": 12383988, "pid": "749c360516ebe533", "uv": 179472, "pname": "百信手机网外链", "nu": 12383988}, {"pv": 7263753, "vv": 960556, "pid": "749c360516ebe533", "uv": 398384, "pname": "航海桌面导航", "nu": 960556}], "day": "29", "month": "11"}, {"year": "2011", "data": [{"pv": 9555887, "vv": 1046894, "pid": "749c360516ebe533", "uv": 383029, "pname": "mobile优酷友情合作", "nu": 1046894}, {"pv": 17147726, "vv": 2320942, "pid": "749c360516ebe533", "uv": 530421, "pname": "3G门户G浏览器", "nu": 2320942}, {"pv": 26100803, "vv": 13301933, "pid": "749c360516ebe533", "uv": 184759, "pname": "百信手机网外链", "nu": 13301933}, {"pv": 7837226, "vv": 1065946, "pid": "749c360516ebe533", "uv": 462942, "pname": "航海桌面导航", "nu": 1065946}], "day": "30", "month": "11"}, {"year": "2011", "data": [{"pv": 9191284, "vv": 997910, "pid": "749c360516ebe533", "uv": 378109, "pname": "mobile优酷友情合作", "nu": 997910}, {"pv": 17538103, "vv": 2212043, "pid": "749c360516ebe533", "uv": 508844, "pname": "3G门户G浏览器", "nu": 2212043}, {"pv": 23764115, "vv": 12167367, "pid": "749c360516ebe533", "uv": 182590, "pname": "百信手机网外链", "nu": 12167367}, {"pv": 7159327, "vv": 898155, "pid": "749c360516ebe533", "uv": 455937, "pname": "航海桌面导航", "nu": 898155}], "day": "01", "month": "12"}, {"year": "2011", "data": [{"pv": 8746707, "vv": 944695, "pid": "749c360516ebe533", "uv": 369130, "pname": "mobile优酷友情合作", "nu": 944695}, {"pv": 20920133, "vv": 2218533, "pid": "749c360516ebe533", "uv": 515960, "pname": "3G门户G浏览器", "nu": 2218533}, {"pv": 22158746, "vv": 11385687, "pid": "749c360516ebe533", "uv": 176859, "pname": "百信手机网外链", "nu": 11385687}, {"pv": 6573226, "vv": 820262, "pid": "749c360516ebe533", "uv": 429128, "pname": "航海桌面导航", "nu": 820262}], "day": "02", "month": "12"}, {"year": "2011", "data": [{"pv": 9729478, "vv": 1075297, "pid": "749c360516ebe533", "uv": 399538, "pname": "mobile优酷友情合作", "nu": 1075297}, {"pv": 25240817, "vv": 2707561, "pid": "749c360516ebe533", "uv": 601559, "pname": "3G门户G浏览器", "nu": 2707561}, {"pv": 23853896, "vv": 12166467, "pid": "749c360516ebe533", "uv": 197107, "pname": "百信手机网外链", "nu": 12166467}, {"pv": 6951907, "vv": 883009, "pid": "749c360516ebe533", "uv": 448783, "pname": "航海桌面导航", "nu": 883009}], "day": "03", "month": "12"}, {"year": "2011", "data": [{"pv": 6553890, "vv": 1000655, "pid": "749c360516ebe533", "uv": 410977, "pname": "mobile优酷友情合作", "nu": 1000655}, {"pv": 26534414, "vv": 2949384, "pid": "749c360516ebe533", "uv": 657332, "pname": "3G门户G浏览器", "nu": 2949384}, {"pv": 20255624, "vv": 9431825, "pid": "749c360516ebe533", "uv": 202836, "pname": "百信手机网外链", "nu": 9431825}, {"pv": 6194333, "vv": 709191, "pid": "749c360516ebe533", "uv": 401298, "pname": "航海桌面导航", "nu": 709191}], "day": "04", "month": "12"}, {"year": "2011", "data": [{"pv": 7504068, "vv": 931829, "pid": "749c360516ebe533", "uv": 372472, "pname": "mobile优酷友情合作", "nu": 931829}, {"pv": 26728075, "vv": 2215458, "pid": "749c360516ebe533", "uv": 556967, "pname": "3G门户G浏览器", "nu": 2215458}, {"pv": 22015006, "vv": 11073917, "pid": "749c360516ebe533", "uv": 181164, "pname": "百信手机网外链", "nu": 11073917}, {"pv": 6398145, "vv": 790298, "pid": "749c360516ebe533", "uv": 404533, "pname": "航海桌面导航", "nu": 790298}], "day": "05", "month": "12"}, {"year": "2011", "data": [{"pv": 9102575, "vv": 1062570, "pid": "749c360516ebe533", "uv": 375255, "pname": "mobile优酷友情合作", "nu": 1062570}, {"pv": 22657579, "vv": 2378515, "pid": "749c360516ebe533", "uv": 519891, "pname": "3G门户G浏览器", "nu": 2378515}, {"pv": 25411025, "vv": 12908338, "pid": "749c360516ebe533", "uv": 183900, "pname": "百信手机网外链", "nu": 12908338}, {"pv": 7267270, "vv": 921841, "pid": "749c360516ebe533", "uv": 431079, "pname": "航海桌面导航", "nu": 921841}], "day": "06", "month": "12"}, {"year": "2011", "data": [{"pv": 9323365, "vv": 1093411, "pid": "749c360516ebe533", "uv": 380987, "pname": "mobile优酷友情合作", "nu": 1093411}, {"pv": 27118625, "vv": 2324804, "pid": "749c360516ebe533", "uv": 509508, "pname": "3G门户G浏览器", "nu": 2324804}, {"pv": 27824591, "vv": 13416846, "pid": "749c360516ebe533", "uv": 201491, "pname": "百信手机网外链", "nu": 13416846}, {"pv": 8086168, "vv": 1000817, "pid": "749c360516ebe533", "uv": 442779, "pname": "航海桌面导航", "nu": 1000817}], "day": "07", "month": "12"}, {"year": "2011", "data": [{"pv": 9038836, "vv": 1063200, "pid": "749c360516ebe533", "uv": 373748, "pname": "mobile优酷友情合作", "nu": 1063200}, {"pv": 17233370, "vv": 2134431, "pid": "749c360516ebe533", "uv": 508371, "pname": "3G门户G浏览器", "nu": 2134431}, {"pv": 26251026, "vv": 12625185, "pid": "749c360516ebe533", "uv": 205565, "pname": "百信手机网外链", "nu": 12625185}, {"pv": 7313013, "vv": 902782, "pid": "749c360516ebe533", "uv": 411684, "pname": "航海桌面导航", "nu": 902782}], "day": "08", "month": "12"}, {"year": "2011", "data": [{"pv": 8363367, "vv": 1045071, "pid": "749c360516ebe533", "uv": 371220, "pname": "mobile优酷友情合作", "nu": 1045071}, {"pv": 17447010, "vv": 2132177, "pid": "749c360516ebe533", "uv": 481472, "pname": "3G门户G浏览器", "nu": 2132177}, {"pv": 23802977, "vv": 11606653, "pid": "749c360516ebe533", "uv": 208438, "pname": "百信手机网外链", "nu": 11606653}, {"pv": 6693872, "vv": 937565, "pid": "749c360516ebe533", "uv": 398948, "pname": "航海桌面导航", "nu": 937565}], "day": "09", "month": "12"}, {"year": "2011", "data": [{"pv": 9525712, "vv": 1127967, "pid": "749c360516ebe533", "uv": 399203, "pname": "mobile优酷友情合作", "nu": 1127967}, {"pv": 21035193, "vv": 2485316, "pid": "749c360516ebe533", "uv": 550803, "pname": "3G门户G浏览器", "nu": 2485316}, {"pv": 26775777, "vv": 12982224, "pid": "749c360516ebe533", "uv": 239143, "pname": "百信手机网外链", "nu": 12982224}, {"pv": 6780413, "vv": 835476, "pid": "749c360516ebe533", "uv": 422789, "pname": "航海桌面导航", "nu": 835476}], "day": "10", "month": "12"}, {"year": "2011", "data": [{"pv": 9700728, "vv": 1156906, "pid": "749c360516ebe533", "uv": 404359, "pname": "mobile优酷友情合作", "nu": 1156906}, {"pv": 18842952, "vv": 2470392, "pid": "749c360516ebe533", "uv": 546650, "pname": "3G门户G浏览器", "nu": 2470392}, {"pv": 27251966, "vv": 13014231, "pid": "749c360516ebe533", "uv": 249194, "pname": "百信手机网外链", "nu": 13014231}, {"pv": 6725342, "vv": 812185, "pid": "749c360516ebe533", "uv": 417197, "pname": "航海桌面导航", "nu": 812185}], "day": "11", "month": "12"}, {"year": "2011", "data": [{"pv": 8318720, "vv": 1010236, "pid": "749c360516ebe533", "uv": 361017, "pname": "mobile优酷友情合作", "nu": 1010236}, {"pv": 12905152, "vv": 1890186, "pid": "749c360516ebe533", "uv": 446430, "pname": "3G门户G浏览器", "nu": 1890186}, {"pv": 24365788, "vv": 11680267, "pid": "749c360516ebe533", "uv": 216079, "pname": "百信手机网外链", "nu": 11680267}, {"pv": 6134800, "vv": 730836, "pid": "749c360516ebe533", "uv": 388648, "pname": "航海桌面导航", "nu": 730836}], "day": "12", "month": "12"}, {"year": "2011", "data": [{"pv": 8689454, "vv": 1069416, "pid": "749c360516ebe533", "uv": 371320, "pname": "mobile优酷友情合作", "nu": 1069416}, {"pv": 14424782, "vv": 1908065, "pid": "749c360516ebe533", "uv": 451278, "pname": "3G门户G浏览器", "nu": 1908065}, {"pv": 25477197, "vv": 12223510, "pid": "749c360516ebe533", "uv": 223816, "pname": "百信手机网外链", "nu": 12223510}, {"pv": 6353900, "vv": 764547, "pid": "749c360516ebe533", "uv": 396732, "pname": "航海桌面导航", "nu": 764547}], "day": "13", "month": "12"}, {"year": "2011", "data": [{"pv": 8762564, "vv": 1100199, "pid": "749c360516ebe533", "uv": 375636, "pname": "mobile优酷友情合作", "nu": 1100199}, {"pv": 15790426, "vv": 1933915, "pid": "749c360516ebe533", "uv": 447268, "pname": "3G门户G浏览器", "nu": 1933915}, {"pv": 25910457, "vv": 12576917, "pid": "749c360516ebe533", "uv": 226918, "pname": "百信手机网外链", "nu": 12576917}, {"pv": 6392187, "vv": 788946, "pid": "749c360516ebe533", "uv": 395650, "pname": "航海桌面导航", "nu": 788946}], "day": "14", "month": "12"}, {"year": "2011", "data": [{"pv": 8648849, "vv": 1117817, "pid": "749c360516ebe533", "uv": 381679, "pname": "mobile优酷友情合作", "nu": 1117817}, {"pv": 14460326, "vv": 1993575, "pid": "749c360516ebe533", "uv": 458094, "pname": "3G门户G浏览器", "nu": 1993575}, {"pv": 26274120, "vv": 12717041, "pid": "749c360516ebe533", "uv": 234441, "pname": "百信手机网外链", "nu": 12717041}, {"pv": 6442292, "vv": 801841, "pid": "749c360516ebe533", "uv": 404290, "pname": "航海桌面导航", "nu": 801841}], "day": "15", "month": "12"}, {"year": "2011", "data": [{"pv": 8974934, "vv": 1135333, "pid": "749c360516ebe533", "uv": 384968, "pname": "mobile优酷友情合作", "nu": 1135333}, {"pv": 14935738, "vv": 1968406, "pid": "749c360516ebe533", "uv": 460287, "pname": "3G门户G浏览器", "nu": 1968406}, {"pv": 26481000, "vv": 12890380, "pid": "749c360516ebe533", "uv": 238530, "pname": "百信手机网外链", "nu": 12890380}, {"pv": 6517663, "vv": 807037, "pid": "749c360516ebe533", "uv": 411766, "pname": "航海桌面导航", "nu": 807037}], "day": "16", "month": "12"}, {"year": "2011", "data": [{"pv": 10026462, "vv": 1236730, "pid": "749c360516ebe533", "uv": 420494, "pname": "mobile优酷友情合作", "nu": 1236730}, {"pv": 17893920, "vv": 2373075, "pid": "749c360516ebe533", "uv": 533109, "pname": "3G门户G浏览器", "nu": 2373075}, {"pv": 29427041, "vv": 14269110, "pid": "749c360516ebe533", "uv": 276155, "pname": "百信手机网外链", "nu": 14269110}, {"pv": 7106385, "vv": 902434, "pid": "749c360516ebe533", "uv": 441599, "pname": "航海桌面导航", "nu": 902434}], "day": "17", "month": "12"}, {"year": "2011", "data": [{"pv": 10245069, "vv": 1289253, "pid": "749c360516ebe533", "uv": 428359, "pname": "mobile优酷友情合作", "nu": 1289253}, {"pv": 16366469, "vv": 2416836, "pid": "749c360516ebe533", "uv": 535023, "pname": "3G门户G浏览器", "nu": 2416836}, {"pv": 30343887, "vv": 14817690, "pid": "749c360516ebe533", "uv": 285791, "pname": "百信手机网外链", "nu": 14817690}, {"pv": 7162604, "vv": 903269, "pid": "749c360516ebe533", "uv": 451413, "pname": "航海桌面导航", "nu": 903269}], "day": "18", "month": "12"}, {"year": "2011", "data": [{"pv": 8928252, "vv": 1110394, "pid": "749c360516ebe533", "uv": 389020, "pname": "mobile优酷友情合作", "nu": 1110394}, {"pv": 12521422, "vv": 1880090, "pid": "749c360516ebe533", "uv": 448916, "pname": "3G门户G浏览器", "nu": 1880090}, {"pv": 26264948, "vv": 12763681, "pid": "749c360516ebe533", "uv": 248776, "pname": "百信手机网外链", "nu": 12763681}, {"pv": 6481151, "vv": 803660, "pid": "749c360516ebe533", "uv": 407620, "pname": "航海桌面导航", "nu": 803660}], "day": "19", "month": "12"}]}
        self.write({"status":"success","items":all_data})
        self.finish()
        
class FactoryName(BaseHandler):
    def get(self):
        #test = {"items":[{"os":"Android", "fname":"华为 huawei", "fid":"2"},{"os":"Android", "fname":"多普达 HTC", "fid":"5"},{"os":"Android", "fname":"联想 lenovo", "fid":"10"},{"os":"Android", "fname":"三星 SAMSUNG", "fid":"12"},{"os":"Android", "fname":"优酷 youku", "fid":"14"},{"os":"Android", "fname":"天宇 K-Touch", "fid":"16"},{"os":"Android", "fname":"点心OS Dianxin", "fid":"29"},{"os":"Android", "fname":"LG", "fid":"75"},{"os":"Android", "fname":"MOTO", "fid":"77"},{"os":"Android", "fname":"oppo", "fid":"110"},{"os":"Android", "fname":"TCL", "fid":"144"},{"os":"Android", "fname":"索尼", "fid":"159"},{"os":"Android", "fname":"海尔", "fid":"161"},{"os":"Android", "fname":"海尔", "fid":"161"},{"os":"Android", "fname":"步步高", "fid":"170"},{"os":"Android", "fname":"酷派", "fid":"177"},{"os":"Android", "fname":"壹人壹本", "fid":"181"},{"os":"Android", "fname":"小米科技", "fid":"184"},{"os":"Android", "fname":"炬力", "fid":"185"},{"os":"Android", "fname":"康佳", "fid":"187"},{"os":"Android", "fname":"金立", "fid":"194"},{"os":"Android", "fname":"天语", "fid":"196"},{"os":"Android", "fname":"宏基", "fid":"197"},{"os":"Android", "fname":"广东移动", "fid":"198"},{"os":"Android", "fname":"海信", "fid":"199"},{"os":"Android", "fname":"万利达", "fid":"202"},{"os":"Android", "fname":"驰为", "fid":"205"},{"os":"Android", "fname":"博泰", "fid":"206"},{"os":"Android", "fname":"网合", "fid":"208"},{"os":"Android", "fname":"中兴", "fid":"211"},{"os":"Android", "fname":"杰拓", "fid":"213"},{"os":"Android", "fname":"大唐", "fid":"215"},{"os":"Android", "fname":"大唐", "fid":"215"},{"os":"Android", "fname":"高通", "fid":"217"},{"os":"bada", "fname":"三星 SAMSUNG", "fid":"12"},{"os":"java", "fname":"天奕达", "fid":"212"},{"os":"MTK", "fname":"MTK MTK", "fid":"18"},{"os":"MTK", "fname":"oppo", "fid":"110"},{"os":"MTK", "fname":"TCL", "fid":"144"},{"os":"MTK", "fname":"步步高", "fid":"170"},{"os":"MTK", "fname":"奥克斯", "fid":"214"},{"os":"Other", "fname":"诺基亚 nokia", "fid":"3"},{"os":"Other", "fname":"黑莓 blackberry", "fid":"26"},{"os":"unknown", "fname":"华为 huawei", "fid":"2"},{"os":"unknown", "fname":"爱国者 aigo", "fid":"20"},{"os":"unknown", "fname":"TCL", "fid":"144"},{"os":"Windows Phone", "fname":"多普达 HTC", "fid":"5"},{"os":"Windows Phone", "fname":"三星 SAMSUNG", "fid":"12"},{"os":"Windows Phone", "fname":"天宇 K-Touch", "fid":"16"},{"os":"Windows Phone", "fname":"通用", "fid":"140"}]}
        resjson = clientcache.getPidFactoryName()
        
        self.write(resjson)
        self.finish()

class FactoryAnalytics(BaseHandler):
    def get(self): 
        parnter_json = clientcache.getParnter()
        
        _fid = self.get_argument('fid', '').strip()
        _osid = self.get_argument('osid','').strip()
        _staDate = self.get_argument('staDate', '2011/11/13').strip()
        _pd = self.get_argument('pd', '7').strip()
        
        if not _pd :
            _pd = 7
        else:
            _pd = int(_pd)
        
        if not _fid or not _osid :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        st_time = ''
        et_time = ''
        
        if not _staDate:
            _time = datetime.datetime.now() + datetime.timedelta(days=-1)
            st_time = (_time + datetime.timedelta(days=-_pd)).strftime( '%Y%m%d')
            et_time = _time.strftime( '%Y%m%d')
        else :
            try:
                #st_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
                _time = datetime.datetime.strptime(_staDate, '%Y/%m/%d')
            except :
                self.send_error(400, desc='invalid argument')
                #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
                #self.finish()
                return
            st_time= _time.strftime( '%Y%m%d')
            et_time = (_time + datetime.timedelta(days=_pd)).strftime( '%Y%m%d')
        
        all_data = []
        results = {}
        os_data = []

        db = connectMySQL.connMySQL_stat()
        c=db.cursor()        
        sql = "SELECT date, pv, uv, vv, newuser, pid  FROM `stat_day_factory` WHERE date>= '{0}' and date < '{1}' and  f_id={2} and os_id={3} and partner_mode=1 and pid_stat=1 order by date".format(st_time, et_time, _fid, _osid)
        logging.info(sql)
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        for x in res:
            date = str(x[0])
            pv = int(x[1])
            uv = int(x[2])
            vv = int(x[3])
            nu = int(x[4])
            pid = x[5]

            if _tmpdate and _tmpdate != date:
                results['data'] = os_data
                results['year'] = _tmpdate[:4]
                results['month'] = _tmpdate[4:6]
                results['day'] = _tmpdate[6:]
                all_data.append(results)
                results = {}
                os_data = []
            
            os_data.insert(0,{'uv':uv, 'nu':nu, 'pid':pid, 'pname':parnter_json.get(pid,['',''])[0], 'ph':parnter_json.get(pid,['',''])[1]})
            _tmpdate = date
        
        c.close()
        db.close()
        
        if len(os_data) > 0 :
            results['data'] = os_data
            results['year'] = _tmpdate[:4]
            results['month'] = _tmpdate[4:6]
            results['day'] = _tmpdate[6:]
            all_data.append(results)
            results = {}
            os_data = []
        
        #print all_data
        #test = {"items": [{"year": "2011", "data": [{"pv": 8706151, "vv": 883726, "pid": "749c360516ebe533", "uv": 379018, "pname": "mobile优酷友情合作", "nu": 883726}, {"pv": 23036219, "vv": 2646085, "pid": "749c360516ebe533", "uv": 618283, "pname": "3G门户G浏览器", "nu": 2646085}, {"pv": 20426465, "vv": 10197784, "pid": "749c360516ebe533", "uv": 177077, "pname": "百信手机网外链", "nu": 10197784}, {"pv": 5984380, "vv": 675899.0, "pid": "749c360516ebe533", "uv": 380541, "pname": "航海桌面导航", "nu": 675899.0}], "day": "20", "month": "11"}, {"year": "2011", "data": [{"pv": 7738457, "vv": 788236, "pid": "749c360516ebe533", "uv": 344861, "pname": "mobile优酷友情合作", "nu": 788236}, {"pv": 19551225, "vv": 2101659, "pid": "749c360516ebe533", "uv": 510932, "pname": "3G门户G浏览器", "nu": 2101659}, {"pv": 18427618, "vv": 9257357, "pid": "749c360516ebe533", "uv": 157920, "pname": "百信手机网外链", "nu": 9257357}, {"pv": 5501749, "vv": 607562.0, "pid": "749c360516ebe533", "uv": 346352, "pname": "航海桌面导航", "nu": 607562.0}], "day": "21", "month": "11"}, {"year": "2011", "data": [{"pv": 7954177, "vv": 815959, "pid": "749c360516ebe533", "uv": 349300, "pname": "mobile优酷友情合作", "nu": 815959}, {"pv": 17299241, "vv": 2118076, "pid": "749c360516ebe533", "uv": 501143, "pname": "3G门户G浏览器", "nu": 2118076}, {"pv": 20366128, "vv": 10192610, "pid": "749c360516ebe533", "uv": 162041, "pname": "百信手机网外链", "nu": 10192610}, {"pv": 6219346, "vv": 899602, "pid": "749c360516ebe533", "uv": 363954, "pname": "航海桌面导航", "nu": 899602}], "day": "22", "month": "11"}, {"year": "2011", "data": [{"pv": 7977166, "vv": 842794, "pid": "749c360516ebe533", "uv": 348040, "pname": "mobile优酷友情合作", "nu": 842794}, {"pv": 16584716, "vv": 2097957, "pid": "749c360516ebe533", "uv": 490854, "pname": "3G门户G浏览器", "nu": 2097957}, {"pv": 20198366, "vv": 10391665, "pid": "749c360516ebe533", "uv": 161251, "pname": "百信手机网外链", "nu": 10391665}, {"pv": 5815811, "vv": 696805, "pid": "749c360516ebe533", "uv": 364588, "pname": "航海桌面导航", "nu": 696805}], "day": "23", "month": "11"}, {"year": "2011", "data": [{"pv": 7903879, "vv": 847485, "pid": "749c360516ebe533", "uv": 346739, "pname": "mobile优酷友情合作", "nu": 847485}, {"pv": 15089805, "vv": 2072196, "pid": "749c360516ebe533", "uv": 486514, "pname": "3G门户G浏览器", "nu": 2072196}, {"pv": 21005978, "vv": 10992027, "pid": "749c360516ebe533", "uv": 160747, "pname": "百信手机网外链", "nu": 10992027}, {"pv": 5907725, "vv": 714918, "pid": "749c360516ebe533", "uv": 365872, "pname": "航海桌面导航", "nu": 714918}], "day": "24", "month": "11"}, {"year": "2011", "data": [{"pv": 8036462, "vv": 859703, "pid": "749c360516ebe533", "uv": 351217, "pname": "mobile优酷友情合作", "nu": 859703}, {"pv": 16574256, "vv": 2147187, "pid": "749c360516ebe533", "uv": 508284, "pname": "3G门户G浏览器", "nu": 2147187}, {"pv": 20814242, "vv": 10802160, "pid": "749c360516ebe533", "uv": 166903, "pname": "百信手机网外链", "nu": 10802160}, {"pv": 5869896, "vv": 742478, "pid": "749c360516ebe533", "uv": 370612, "pname": "航海桌面导航", "nu": 742478}], "day": "25", "month": "11"}, {"year": "2011", "data": [{"pv": 9069765, "vv": 972571, "pid": "749c360516ebe533", "uv": 387447, "pname": "mobile优酷友情合作", "nu": 972571}, {"pv": 19110457, "vv": 2686480, "pid": "749c360516ebe533", "uv": 607180, "pname": "3G门户G浏览器", "nu": 2686480}, {"pv": 22990850, "vv": 12016920, "pid": "749c360516ebe533", "uv": 189509, "pname": "百信手机网外链", "nu": 12016920}, {"pv": 6409802, "vv": 819128, "pid": "749c360516ebe533", "uv": 408271, "pname": "航海桌面导航", "nu": 819128}], "day": "26", "month": "11"}, {"year": "2011", "data": [{"pv": 5164029, "vv": 905470, "pid": "749c360516ebe533", "uv": 402251, "pname": "mobile优酷友情合作", "nu": 905470}, {"pv": 22663416, "vv": 2959138, "pid": "749c360516ebe533", "uv": 625509, "pname": "3G门户G浏览器", "nu": 2959138}, {"pv": 22769949, "vv": 12005075, "pid": "749c360516ebe533", "uv": 194499, "pname": "百信手机网外链", "nu": 12005075}, {"pv": 6496387, "vv": 824110, "pid": "749c360516ebe533", "uv": 405406, "pname": "航海桌面导航", "nu": 824110}], "day": "27", "month": "11"}, {"year": "2011", "data": [{"pv": 7883670, "vv": 923891, "pid": "749c360516ebe533", "uv": 364508, "pname": "mobile优酷友情合作", "nu": 923891}, {"pv": 16813831, "vv": 2227148, "pid": "749c360516ebe533", "uv": 523621, "pname": "3G门户G浏览器", "nu": 2227148}, {"pv": 21876510, "vv": 11389919, "pid": "749c360516ebe533", "uv": 177008, "pname": "百信手机网外链", "nu": 11389919}, {"pv": 6182231, "vv": 786666, "pid": "749c360516ebe533", "uv": 377465, "pname": "航海桌面导航", "nu": 786666}], "day": "28", "month": "11"}, {"year": "2011", "data": [{"pv": 8932697, "vv": 979964, "pid": "749c360516ebe533", "uv": 371687, "pname": "mobile优酷友情合作", "nu": 979964}, {"pv": 18295828, "vv": 2271816, "pid": "749c360516ebe533", "uv": 529236, "pname": "3G门户G浏览器", "nu": 2271816}, {"pv": 24524788, "vv": 12383988, "pid": "749c360516ebe533", "uv": 179472, "pname": "百信手机网外链", "nu": 12383988}, {"pv": 7263753, "vv": 960556, "pid": "749c360516ebe533", "uv": 398384, "pname": "航海桌面导航", "nu": 960556}], "day": "29", "month": "11"}, {"year": "2011", "data": [{"pv": 9555887, "vv": 1046894, "pid": "749c360516ebe533", "uv": 383029, "pname": "mobile优酷友情合作", "nu": 1046894}, {"pv": 17147726, "vv": 2320942, "pid": "749c360516ebe533", "uv": 530421, "pname": "3G门户G浏览器", "nu": 2320942}, {"pv": 26100803, "vv": 13301933, "pid": "749c360516ebe533", "uv": 184759, "pname": "百信手机网外链", "nu": 13301933}, {"pv": 7837226, "vv": 1065946, "pid": "749c360516ebe533", "uv": 462942, "pname": "航海桌面导航", "nu": 1065946}], "day": "30", "month": "11"}, {"year": "2011", "data": [{"pv": 9191284, "vv": 997910, "pid": "749c360516ebe533", "uv": 378109, "pname": "mobile优酷友情合作", "nu": 997910}, {"pv": 17538103, "vv": 2212043, "pid": "749c360516ebe533", "uv": 508844, "pname": "3G门户G浏览器", "nu": 2212043}, {"pv": 23764115, "vv": 12167367, "pid": "749c360516ebe533", "uv": 182590, "pname": "百信手机网外链", "nu": 12167367}, {"pv": 7159327, "vv": 898155, "pid": "749c360516ebe533", "uv": 455937, "pname": "航海桌面导航", "nu": 898155}], "day": "01", "month": "12"}, {"year": "2011", "data": [{"pv": 8746707, "vv": 944695, "pid": "749c360516ebe533", "uv": 369130, "pname": "mobile优酷友情合作", "nu": 944695}, {"pv": 20920133, "vv": 2218533, "pid": "749c360516ebe533", "uv": 515960, "pname": "3G门户G浏览器", "nu": 2218533}, {"pv": 22158746, "vv": 11385687, "pid": "749c360516ebe533", "uv": 176859, "pname": "百信手机网外链", "nu": 11385687}, {"pv": 6573226, "vv": 820262, "pid": "749c360516ebe533", "uv": 429128, "pname": "航海桌面导航", "nu": 820262}], "day": "02", "month": "12"}, {"year": "2011", "data": [{"pv": 9729478, "vv": 1075297, "pid": "749c360516ebe533", "uv": 399538, "pname": "mobile优酷友情合作", "nu": 1075297}, {"pv": 25240817, "vv": 2707561, "pid": "749c360516ebe533", "uv": 601559, "pname": "3G门户G浏览器", "nu": 2707561}, {"pv": 23853896, "vv": 12166467, "pid": "749c360516ebe533", "uv": 197107, "pname": "百信手机网外链", "nu": 12166467}, {"pv": 6951907, "vv": 883009, "pid": "749c360516ebe533", "uv": 448783, "pname": "航海桌面导航", "nu": 883009}], "day": "03", "month": "12"}, {"year": "2011", "data": [{"pv": 6553890, "vv": 1000655, "pid": "749c360516ebe533", "uv": 410977, "pname": "mobile优酷友情合作", "nu": 1000655}, {"pv": 26534414, "vv": 2949384, "pid": "749c360516ebe533", "uv": 657332, "pname": "3G门户G浏览器", "nu": 2949384}, {"pv": 20255624, "vv": 9431825, "pid": "749c360516ebe533", "uv": 202836, "pname": "百信手机网外链", "nu": 9431825}, {"pv": 6194333, "vv": 709191, "pid": "749c360516ebe533", "uv": 401298, "pname": "航海桌面导航", "nu": 709191}], "day": "04", "month": "12"}, {"year": "2011", "data": [{"pv": 7504068, "vv": 931829, "pid": "749c360516ebe533", "uv": 372472, "pname": "mobile优酷友情合作", "nu": 931829}, {"pv": 26728075, "vv": 2215458, "pid": "749c360516ebe533", "uv": 556967, "pname": "3G门户G浏览器", "nu": 2215458}, {"pv": 22015006, "vv": 11073917, "pid": "749c360516ebe533", "uv": 181164, "pname": "百信手机网外链", "nu": 11073917}, {"pv": 6398145, "vv": 790298, "pid": "749c360516ebe533", "uv": 404533, "pname": "航海桌面导航", "nu": 790298}], "day": "05", "month": "12"}, {"year": "2011", "data": [{"pv": 9102575, "vv": 1062570, "pid": "749c360516ebe533", "uv": 375255, "pname": "mobile优酷友情合作", "nu": 1062570}, {"pv": 22657579, "vv": 2378515, "pid": "749c360516ebe533", "uv": 519891, "pname": "3G门户G浏览器", "nu": 2378515}, {"pv": 25411025, "vv": 12908338, "pid": "749c360516ebe533", "uv": 183900, "pname": "百信手机网外链", "nu": 12908338}, {"pv": 7267270, "vv": 921841, "pid": "749c360516ebe533", "uv": 431079, "pname": "航海桌面导航", "nu": 921841}], "day": "06", "month": "12"}, {"year": "2011", "data": [{"pv": 9323365, "vv": 1093411, "pid": "749c360516ebe533", "uv": 380987, "pname": "mobile优酷友情合作", "nu": 1093411}, {"pv": 27118625, "vv": 2324804, "pid": "749c360516ebe533", "uv": 509508, "pname": "3G门户G浏览器", "nu": 2324804}, {"pv": 27824591, "vv": 13416846, "pid": "749c360516ebe533", "uv": 201491, "pname": "百信手机网外链", "nu": 13416846}, {"pv": 8086168, "vv": 1000817, "pid": "749c360516ebe533", "uv": 442779, "pname": "航海桌面导航", "nu": 1000817}], "day": "07", "month": "12"}, {"year": "2011", "data": [{"pv": 9038836, "vv": 1063200, "pid": "749c360516ebe533", "uv": 373748, "pname": "mobile优酷友情合作", "nu": 1063200}, {"pv": 17233370, "vv": 2134431, "pid": "749c360516ebe533", "uv": 508371, "pname": "3G门户G浏览器", "nu": 2134431}, {"pv": 26251026, "vv": 12625185, "pid": "749c360516ebe533", "uv": 205565, "pname": "百信手机网外链", "nu": 12625185}, {"pv": 7313013, "vv": 902782, "pid": "749c360516ebe533", "uv": 411684, "pname": "航海桌面导航", "nu": 902782}], "day": "08", "month": "12"}, {"year": "2011", "data": [{"pv": 8363367, "vv": 1045071, "pid": "749c360516ebe533", "uv": 371220, "pname": "mobile优酷友情合作", "nu": 1045071}, {"pv": 17447010, "vv": 2132177, "pid": "749c360516ebe533", "uv": 481472, "pname": "3G门户G浏览器", "nu": 2132177}, {"pv": 23802977, "vv": 11606653, "pid": "749c360516ebe533", "uv": 208438, "pname": "百信手机网外链", "nu": 11606653}, {"pv": 6693872, "vv": 937565, "pid": "749c360516ebe533", "uv": 398948, "pname": "航海桌面导航", "nu": 937565}], "day": "09", "month": "12"}, {"year": "2011", "data": [{"pv": 9525712, "vv": 1127967, "pid": "749c360516ebe533", "uv": 399203, "pname": "mobile优酷友情合作", "nu": 1127967}, {"pv": 21035193, "vv": 2485316, "pid": "749c360516ebe533", "uv": 550803, "pname": "3G门户G浏览器", "nu": 2485316}, {"pv": 26775777, "vv": 12982224, "pid": "749c360516ebe533", "uv": 239143, "pname": "百信手机网外链", "nu": 12982224}, {"pv": 6780413, "vv": 835476, "pid": "749c360516ebe533", "uv": 422789, "pname": "航海桌面导航", "nu": 835476}], "day": "10", "month": "12"}, {"year": "2011", "data": [{"pv": 9700728, "vv": 1156906, "pid": "749c360516ebe533", "uv": 404359, "pname": "mobile优酷友情合作", "nu": 1156906}, {"pv": 18842952, "vv": 2470392, "pid": "749c360516ebe533", "uv": 546650, "pname": "3G门户G浏览器", "nu": 2470392}, {"pv": 27251966, "vv": 13014231, "pid": "749c360516ebe533", "uv": 249194, "pname": "百信手机网外链", "nu": 13014231}, {"pv": 6725342, "vv": 812185, "pid": "749c360516ebe533", "uv": 417197, "pname": "航海桌面导航", "nu": 812185}], "day": "11", "month": "12"}, {"year": "2011", "data": [{"pv": 8318720, "vv": 1010236, "pid": "749c360516ebe533", "uv": 361017, "pname": "mobile优酷友情合作", "nu": 1010236}, {"pv": 12905152, "vv": 1890186, "pid": "749c360516ebe533", "uv": 446430, "pname": "3G门户G浏览器", "nu": 1890186}, {"pv": 24365788, "vv": 11680267, "pid": "749c360516ebe533", "uv": 216079, "pname": "百信手机网外链", "nu": 11680267}, {"pv": 6134800, "vv": 730836, "pid": "749c360516ebe533", "uv": 388648, "pname": "航海桌面导航", "nu": 730836}], "day": "12", "month": "12"}, {"year": "2011", "data": [{"pv": 8689454, "vv": 1069416, "pid": "749c360516ebe533", "uv": 371320, "pname": "mobile优酷友情合作", "nu": 1069416}, {"pv": 14424782, "vv": 1908065, "pid": "749c360516ebe533", "uv": 451278, "pname": "3G门户G浏览器", "nu": 1908065}, {"pv": 25477197, "vv": 12223510, "pid": "749c360516ebe533", "uv": 223816, "pname": "百信手机网外链", "nu": 12223510}, {"pv": 6353900, "vv": 764547, "pid": "749c360516ebe533", "uv": 396732, "pname": "航海桌面导航", "nu": 764547}], "day": "13", "month": "12"}, {"year": "2011", "data": [{"pv": 8762564, "vv": 1100199, "pid": "749c360516ebe533", "uv": 375636, "pname": "mobile优酷友情合作", "nu": 1100199}, {"pv": 15790426, "vv": 1933915, "pid": "749c360516ebe533", "uv": 447268, "pname": "3G门户G浏览器", "nu": 1933915}, {"pv": 25910457, "vv": 12576917, "pid": "749c360516ebe533", "uv": 226918, "pname": "百信手机网外链", "nu": 12576917}, {"pv": 6392187, "vv": 788946, "pid": "749c360516ebe533", "uv": 395650, "pname": "航海桌面导航", "nu": 788946}], "day": "14", "month": "12"}, {"year": "2011", "data": [{"pv": 8648849, "vv": 1117817, "pid": "749c360516ebe533", "uv": 381679, "pname": "mobile优酷友情合作", "nu": 1117817}, {"pv": 14460326, "vv": 1993575, "pid": "749c360516ebe533", "uv": 458094, "pname": "3G门户G浏览器", "nu": 1993575}, {"pv": 26274120, "vv": 12717041, "pid": "749c360516ebe533", "uv": 234441, "pname": "百信手机网外链", "nu": 12717041}, {"pv": 6442292, "vv": 801841, "pid": "749c360516ebe533", "uv": 404290, "pname": "航海桌面导航", "nu": 801841}], "day": "15", "month": "12"}, {"year": "2011", "data": [{"pv": 8974934, "vv": 1135333, "pid": "749c360516ebe533", "uv": 384968, "pname": "mobile优酷友情合作", "nu": 1135333}, {"pv": 14935738, "vv": 1968406, "pid": "749c360516ebe533", "uv": 460287, "pname": "3G门户G浏览器", "nu": 1968406}, {"pv": 26481000, "vv": 12890380, "pid": "749c360516ebe533", "uv": 238530, "pname": "百信手机网外链", "nu": 12890380}, {"pv": 6517663, "vv": 807037, "pid": "749c360516ebe533", "uv": 411766, "pname": "航海桌面导航", "nu": 807037}], "day": "16", "month": "12"}, {"year": "2011", "data": [{"pv": 10026462, "vv": 1236730, "pid": "749c360516ebe533", "uv": 420494, "pname": "mobile优酷友情合作", "nu": 1236730}, {"pv": 17893920, "vv": 2373075, "pid": "749c360516ebe533", "uv": 533109, "pname": "3G门户G浏览器", "nu": 2373075}, {"pv": 29427041, "vv": 14269110, "pid": "749c360516ebe533", "uv": 276155, "pname": "百信手机网外链", "nu": 14269110}, {"pv": 7106385, "vv": 902434, "pid": "749c360516ebe533", "uv": 441599, "pname": "航海桌面导航", "nu": 902434}], "day": "17", "month": "12"}, {"year": "2011", "data": [{"pv": 10245069, "vv": 1289253, "pid": "749c360516ebe533", "uv": 428359, "pname": "mobile优酷友情合作", "nu": 1289253}, {"pv": 16366469, "vv": 2416836, "pid": "749c360516ebe533", "uv": 535023, "pname": "3G门户G浏览器", "nu": 2416836}, {"pv": 30343887, "vv": 14817690, "pid": "749c360516ebe533", "uv": 285791, "pname": "百信手机网外链", "nu": 14817690}, {"pv": 7162604, "vv": 903269, "pid": "749c360516ebe533", "uv": 451413, "pname": "航海桌面导航", "nu": 903269}], "day": "18", "month": "12"}, {"year": "2011", "data": [{"pv": 8928252, "vv": 1110394, "pid": "749c360516ebe533", "uv": 389020, "pname": "mobile优酷友情合作", "nu": 1110394}, {"pv": 12521422, "vv": 1880090, "pid": "749c360516ebe533", "uv": 448916, "pname": "3G门户G浏览器", "nu": 1880090}, {"pv": 26264948, "vv": 12763681, "pid": "749c360516ebe533", "uv": 248776, "pname": "百信手机网外链", "nu": 12763681}, {"pv": 6481151, "vv": 803660, "pid": "749c360516ebe533", "uv": 407620, "pname": "航海桌面导航", "nu": 803660}], "day": "19", "month": "12"}]}
        self.write({"status":"success", "items":all_data})
        self.finish()

class FactoryOSComp(BaseHandler):
    def get(self): 
        os_json = clientcache.getOS()
        
        _fid = self.get_argument('fid', '').strip()
        _period = self.get_argument('period', 'w').strip()
        
        _pd = 0
        if _period == 'm':
            _pd = 30
        elif _period == 'q':
            _pd = 90
        elif _period == 'y':
            _pd = 365
        else:
            _pd = 7
            
        if not _fid:
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        _time = datetime.datetime.now() + datetime.timedelta(days=0)
        st_time = (_time + datetime.timedelta(days=-_pd)).strftime( '%Y%m%d')
        et_time = _time.strftime( '%Y%m%d')

        #print 's:{0} , e:{1}'.format(st_time, et_time)
        
        all_data = []
        results = {}
        os_data = []

        db = connectMySQL.connMySQL_stat()
        c=db.cursor()        
        sql = "SELECT os_id, sum(newuser)  FROM `stat_day_factory` WHERE date>= '{0}' and date < '{1}' and  f_id={2} and partner_mode=1 and pid_stat=1 group by os_id".format(st_time, et_time, _fid)
        logging.info(sql)
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        for x in res:
            #print 'tmpdate:{0}'.format(_tmpdate)
            
            os = os_json.get(x[0],'')
            tu = int(x[1])
            
            os_data.insert(0,{'os':os, 'tnu':tu})
        
        c.close()
        db.close()
        
        #print all_data
        #test = {"items": [{"year": "2011", "data": [{"pv": 8706151, "vv": 883726, "pid": "749c360516ebe533", "uv": 379018, "pname": "mobile优酷友情合作", "nu": 883726}, {"pv": 23036219, "vv": 2646085, "pid": "749c360516ebe533", "uv": 618283, "pname": "3G门户G浏览器", "nu": 2646085}, {"pv": 20426465, "vv": 10197784, "pid": "749c360516ebe533", "uv": 177077, "pname": "百信手机网外链", "nu": 10197784}, {"pv": 5984380, "vv": 675899.0, "pid": "749c360516ebe533", "uv": 380541, "pname": "航海桌面导航", "nu": 675899.0}], "day": "20", "month": "11"}, {"year": "2011", "data": [{"pv": 7738457, "vv": 788236, "pid": "749c360516ebe533", "uv": 344861, "pname": "mobile优酷友情合作", "nu": 788236}, {"pv": 19551225, "vv": 2101659, "pid": "749c360516ebe533", "uv": 510932, "pname": "3G门户G浏览器", "nu": 2101659}, {"pv": 18427618, "vv": 9257357, "pid": "749c360516ebe533", "uv": 157920, "pname": "百信手机网外链", "nu": 9257357}, {"pv": 5501749, "vv": 607562.0, "pid": "749c360516ebe533", "uv": 346352, "pname": "航海桌面导航", "nu": 607562.0}], "day": "21", "month": "11"}, {"year": "2011", "data": [{"pv": 7954177, "vv": 815959, "pid": "749c360516ebe533", "uv": 349300, "pname": "mobile优酷友情合作", "nu": 815959}, {"pv": 17299241, "vv": 2118076, "pid": "749c360516ebe533", "uv": 501143, "pname": "3G门户G浏览器", "nu": 2118076}, {"pv": 20366128, "vv": 10192610, "pid": "749c360516ebe533", "uv": 162041, "pname": "百信手机网外链", "nu": 10192610}, {"pv": 6219346, "vv": 899602, "pid": "749c360516ebe533", "uv": 363954, "pname": "航海桌面导航", "nu": 899602}], "day": "22", "month": "11"}, {"year": "2011", "data": [{"pv": 7977166, "vv": 842794, "pid": "749c360516ebe533", "uv": 348040, "pname": "mobile优酷友情合作", "nu": 842794}, {"pv": 16584716, "vv": 2097957, "pid": "749c360516ebe533", "uv": 490854, "pname": "3G门户G浏览器", "nu": 2097957}, {"pv": 20198366, "vv": 10391665, "pid": "749c360516ebe533", "uv": 161251, "pname": "百信手机网外链", "nu": 10391665}, {"pv": 5815811, "vv": 696805, "pid": "749c360516ebe533", "uv": 364588, "pname": "航海桌面导航", "nu": 696805}], "day": "23", "month": "11"}, {"year": "2011", "data": [{"pv": 7903879, "vv": 847485, "pid": "749c360516ebe533", "uv": 346739, "pname": "mobile优酷友情合作", "nu": 847485}, {"pv": 15089805, "vv": 2072196, "pid": "749c360516ebe533", "uv": 486514, "pname": "3G门户G浏览器", "nu": 2072196}, {"pv": 21005978, "vv": 10992027, "pid": "749c360516ebe533", "uv": 160747, "pname": "百信手机网外链", "nu": 10992027}, {"pv": 5907725, "vv": 714918, "pid": "749c360516ebe533", "uv": 365872, "pname": "航海桌面导航", "nu": 714918}], "day": "24", "month": "11"}, {"year": "2011", "data": [{"pv": 8036462, "vv": 859703, "pid": "749c360516ebe533", "uv": 351217, "pname": "mobile优酷友情合作", "nu": 859703}, {"pv": 16574256, "vv": 2147187, "pid": "749c360516ebe533", "uv": 508284, "pname": "3G门户G浏览器", "nu": 2147187}, {"pv": 20814242, "vv": 10802160, "pid": "749c360516ebe533", "uv": 166903, "pname": "百信手机网外链", "nu": 10802160}, {"pv": 5869896, "vv": 742478, "pid": "749c360516ebe533", "uv": 370612, "pname": "航海桌面导航", "nu": 742478}], "day": "25", "month": "11"}, {"year": "2011", "data": [{"pv": 9069765, "vv": 972571, "pid": "749c360516ebe533", "uv": 387447, "pname": "mobile优酷友情合作", "nu": 972571}, {"pv": 19110457, "vv": 2686480, "pid": "749c360516ebe533", "uv": 607180, "pname": "3G门户G浏览器", "nu": 2686480}, {"pv": 22990850, "vv": 12016920, "pid": "749c360516ebe533", "uv": 189509, "pname": "百信手机网外链", "nu": 12016920}, {"pv": 6409802, "vv": 819128, "pid": "749c360516ebe533", "uv": 408271, "pname": "航海桌面导航", "nu": 819128}], "day": "26", "month": "11"}, {"year": "2011", "data": [{"pv": 5164029, "vv": 905470, "pid": "749c360516ebe533", "uv": 402251, "pname": "mobile优酷友情合作", "nu": 905470}, {"pv": 22663416, "vv": 2959138, "pid": "749c360516ebe533", "uv": 625509, "pname": "3G门户G浏览器", "nu": 2959138}, {"pv": 22769949, "vv": 12005075, "pid": "749c360516ebe533", "uv": 194499, "pname": "百信手机网外链", "nu": 12005075}, {"pv": 6496387, "vv": 824110, "pid": "749c360516ebe533", "uv": 405406, "pname": "航海桌面导航", "nu": 824110}], "day": "27", "month": "11"}, {"year": "2011", "data": [{"pv": 7883670, "vv": 923891, "pid": "749c360516ebe533", "uv": 364508, "pname": "mobile优酷友情合作", "nu": 923891}, {"pv": 16813831, "vv": 2227148, "pid": "749c360516ebe533", "uv": 523621, "pname": "3G门户G浏览器", "nu": 2227148}, {"pv": 21876510, "vv": 11389919, "pid": "749c360516ebe533", "uv": 177008, "pname": "百信手机网外链", "nu": 11389919}, {"pv": 6182231, "vv": 786666, "pid": "749c360516ebe533", "uv": 377465, "pname": "航海桌面导航", "nu": 786666}], "day": "28", "month": "11"}, {"year": "2011", "data": [{"pv": 8932697, "vv": 979964, "pid": "749c360516ebe533", "uv": 371687, "pname": "mobile优酷友情合作", "nu": 979964}, {"pv": 18295828, "vv": 2271816, "pid": "749c360516ebe533", "uv": 529236, "pname": "3G门户G浏览器", "nu": 2271816}, {"pv": 24524788, "vv": 12383988, "pid": "749c360516ebe533", "uv": 179472, "pname": "百信手机网外链", "nu": 12383988}, {"pv": 7263753, "vv": 960556, "pid": "749c360516ebe533", "uv": 398384, "pname": "航海桌面导航", "nu": 960556}], "day": "29", "month": "11"}, {"year": "2011", "data": [{"pv": 9555887, "vv": 1046894, "pid": "749c360516ebe533", "uv": 383029, "pname": "mobile优酷友情合作", "nu": 1046894}, {"pv": 17147726, "vv": 2320942, "pid": "749c360516ebe533", "uv": 530421, "pname": "3G门户G浏览器", "nu": 2320942}, {"pv": 26100803, "vv": 13301933, "pid": "749c360516ebe533", "uv": 184759, "pname": "百信手机网外链", "nu": 13301933}, {"pv": 7837226, "vv": 1065946, "pid": "749c360516ebe533", "uv": 462942, "pname": "航海桌面导航", "nu": 1065946}], "day": "30", "month": "11"}, {"year": "2011", "data": [{"pv": 9191284, "vv": 997910, "pid": "749c360516ebe533", "uv": 378109, "pname": "mobile优酷友情合作", "nu": 997910}, {"pv": 17538103, "vv": 2212043, "pid": "749c360516ebe533", "uv": 508844, "pname": "3G门户G浏览器", "nu": 2212043}, {"pv": 23764115, "vv": 12167367, "pid": "749c360516ebe533", "uv": 182590, "pname": "百信手机网外链", "nu": 12167367}, {"pv": 7159327, "vv": 898155, "pid": "749c360516ebe533", "uv": 455937, "pname": "航海桌面导航", "nu": 898155}], "day": "01", "month": "12"}, {"year": "2011", "data": [{"pv": 8746707, "vv": 944695, "pid": "749c360516ebe533", "uv": 369130, "pname": "mobile优酷友情合作", "nu": 944695}, {"pv": 20920133, "vv": 2218533, "pid": "749c360516ebe533", "uv": 515960, "pname": "3G门户G浏览器", "nu": 2218533}, {"pv": 22158746, "vv": 11385687, "pid": "749c360516ebe533", "uv": 176859, "pname": "百信手机网外链", "nu": 11385687}, {"pv": 6573226, "vv": 820262, "pid": "749c360516ebe533", "uv": 429128, "pname": "航海桌面导航", "nu": 820262}], "day": "02", "month": "12"}, {"year": "2011", "data": [{"pv": 9729478, "vv": 1075297, "pid": "749c360516ebe533", "uv": 399538, "pname": "mobile优酷友情合作", "nu": 1075297}, {"pv": 25240817, "vv": 2707561, "pid": "749c360516ebe533", "uv": 601559, "pname": "3G门户G浏览器", "nu": 2707561}, {"pv": 23853896, "vv": 12166467, "pid": "749c360516ebe533", "uv": 197107, "pname": "百信手机网外链", "nu": 12166467}, {"pv": 6951907, "vv": 883009, "pid": "749c360516ebe533", "uv": 448783, "pname": "航海桌面导航", "nu": 883009}], "day": "03", "month": "12"}, {"year": "2011", "data": [{"pv": 6553890, "vv": 1000655, "pid": "749c360516ebe533", "uv": 410977, "pname": "mobile优酷友情合作", "nu": 1000655}, {"pv": 26534414, "vv": 2949384, "pid": "749c360516ebe533", "uv": 657332, "pname": "3G门户G浏览器", "nu": 2949384}, {"pv": 20255624, "vv": 9431825, "pid": "749c360516ebe533", "uv": 202836, "pname": "百信手机网外链", "nu": 9431825}, {"pv": 6194333, "vv": 709191, "pid": "749c360516ebe533", "uv": 401298, "pname": "航海桌面导航", "nu": 709191}], "day": "04", "month": "12"}, {"year": "2011", "data": [{"pv": 7504068, "vv": 931829, "pid": "749c360516ebe533", "uv": 372472, "pname": "mobile优酷友情合作", "nu": 931829}, {"pv": 26728075, "vv": 2215458, "pid": "749c360516ebe533", "uv": 556967, "pname": "3G门户G浏览器", "nu": 2215458}, {"pv": 22015006, "vv": 11073917, "pid": "749c360516ebe533", "uv": 181164, "pname": "百信手机网外链", "nu": 11073917}, {"pv": 6398145, "vv": 790298, "pid": "749c360516ebe533", "uv": 404533, "pname": "航海桌面导航", "nu": 790298}], "day": "05", "month": "12"}, {"year": "2011", "data": [{"pv": 9102575, "vv": 1062570, "pid": "749c360516ebe533", "uv": 375255, "pname": "mobile优酷友情合作", "nu": 1062570}, {"pv": 22657579, "vv": 2378515, "pid": "749c360516ebe533", "uv": 519891, "pname": "3G门户G浏览器", "nu": 2378515}, {"pv": 25411025, "vv": 12908338, "pid": "749c360516ebe533", "uv": 183900, "pname": "百信手机网外链", "nu": 12908338}, {"pv": 7267270, "vv": 921841, "pid": "749c360516ebe533", "uv": 431079, "pname": "航海桌面导航", "nu": 921841}], "day": "06", "month": "12"}, {"year": "2011", "data": [{"pv": 9323365, "vv": 1093411, "pid": "749c360516ebe533", "uv": 380987, "pname": "mobile优酷友情合作", "nu": 1093411}, {"pv": 27118625, "vv": 2324804, "pid": "749c360516ebe533", "uv": 509508, "pname": "3G门户G浏览器", "nu": 2324804}, {"pv": 27824591, "vv": 13416846, "pid": "749c360516ebe533", "uv": 201491, "pname": "百信手机网外链", "nu": 13416846}, {"pv": 8086168, "vv": 1000817, "pid": "749c360516ebe533", "uv": 442779, "pname": "航海桌面导航", "nu": 1000817}], "day": "07", "month": "12"}, {"year": "2011", "data": [{"pv": 9038836, "vv": 1063200, "pid": "749c360516ebe533", "uv": 373748, "pname": "mobile优酷友情合作", "nu": 1063200}, {"pv": 17233370, "vv": 2134431, "pid": "749c360516ebe533", "uv": 508371, "pname": "3G门户G浏览器", "nu": 2134431}, {"pv": 26251026, "vv": 12625185, "pid": "749c360516ebe533", "uv": 205565, "pname": "百信手机网外链", "nu": 12625185}, {"pv": 7313013, "vv": 902782, "pid": "749c360516ebe533", "uv": 411684, "pname": "航海桌面导航", "nu": 902782}], "day": "08", "month": "12"}, {"year": "2011", "data": [{"pv": 8363367, "vv": 1045071, "pid": "749c360516ebe533", "uv": 371220, "pname": "mobile优酷友情合作", "nu": 1045071}, {"pv": 17447010, "vv": 2132177, "pid": "749c360516ebe533", "uv": 481472, "pname": "3G门户G浏览器", "nu": 2132177}, {"pv": 23802977, "vv": 11606653, "pid": "749c360516ebe533", "uv": 208438, "pname": "百信手机网外链", "nu": 11606653}, {"pv": 6693872, "vv": 937565, "pid": "749c360516ebe533", "uv": 398948, "pname": "航海桌面导航", "nu": 937565}], "day": "09", "month": "12"}, {"year": "2011", "data": [{"pv": 9525712, "vv": 1127967, "pid": "749c360516ebe533", "uv": 399203, "pname": "mobile优酷友情合作", "nu": 1127967}, {"pv": 21035193, "vv": 2485316, "pid": "749c360516ebe533", "uv": 550803, "pname": "3G门户G浏览器", "nu": 2485316}, {"pv": 26775777, "vv": 12982224, "pid": "749c360516ebe533", "uv": 239143, "pname": "百信手机网外链", "nu": 12982224}, {"pv": 6780413, "vv": 835476, "pid": "749c360516ebe533", "uv": 422789, "pname": "航海桌面导航", "nu": 835476}], "day": "10", "month": "12"}, {"year": "2011", "data": [{"pv": 9700728, "vv": 1156906, "pid": "749c360516ebe533", "uv": 404359, "pname": "mobile优酷友情合作", "nu": 1156906}, {"pv": 18842952, "vv": 2470392, "pid": "749c360516ebe533", "uv": 546650, "pname": "3G门户G浏览器", "nu": 2470392}, {"pv": 27251966, "vv": 13014231, "pid": "749c360516ebe533", "uv": 249194, "pname": "百信手机网外链", "nu": 13014231}, {"pv": 6725342, "vv": 812185, "pid": "749c360516ebe533", "uv": 417197, "pname": "航海桌面导航", "nu": 812185}], "day": "11", "month": "12"}, {"year": "2011", "data": [{"pv": 8318720, "vv": 1010236, "pid": "749c360516ebe533", "uv": 361017, "pname": "mobile优酷友情合作", "nu": 1010236}, {"pv": 12905152, "vv": 1890186, "pid": "749c360516ebe533", "uv": 446430, "pname": "3G门户G浏览器", "nu": 1890186}, {"pv": 24365788, "vv": 11680267, "pid": "749c360516ebe533", "uv": 216079, "pname": "百信手机网外链", "nu": 11680267}, {"pv": 6134800, "vv": 730836, "pid": "749c360516ebe533", "uv": 388648, "pname": "航海桌面导航", "nu": 730836}], "day": "12", "month": "12"}, {"year": "2011", "data": [{"pv": 8689454, "vv": 1069416, "pid": "749c360516ebe533", "uv": 371320, "pname": "mobile优酷友情合作", "nu": 1069416}, {"pv": 14424782, "vv": 1908065, "pid": "749c360516ebe533", "uv": 451278, "pname": "3G门户G浏览器", "nu": 1908065}, {"pv": 25477197, "vv": 12223510, "pid": "749c360516ebe533", "uv": 223816, "pname": "百信手机网外链", "nu": 12223510}, {"pv": 6353900, "vv": 764547, "pid": "749c360516ebe533", "uv": 396732, "pname": "航海桌面导航", "nu": 764547}], "day": "13", "month": "12"}, {"year": "2011", "data": [{"pv": 8762564, "vv": 1100199, "pid": "749c360516ebe533", "uv": 375636, "pname": "mobile优酷友情合作", "nu": 1100199}, {"pv": 15790426, "vv": 1933915, "pid": "749c360516ebe533", "uv": 447268, "pname": "3G门户G浏览器", "nu": 1933915}, {"pv": 25910457, "vv": 12576917, "pid": "749c360516ebe533", "uv": 226918, "pname": "百信手机网外链", "nu": 12576917}, {"pv": 6392187, "vv": 788946, "pid": "749c360516ebe533", "uv": 395650, "pname": "航海桌面导航", "nu": 788946}], "day": "14", "month": "12"}, {"year": "2011", "data": [{"pv": 8648849, "vv": 1117817, "pid": "749c360516ebe533", "uv": 381679, "pname": "mobile优酷友情合作", "nu": 1117817}, {"pv": 14460326, "vv": 1993575, "pid": "749c360516ebe533", "uv": 458094, "pname": "3G门户G浏览器", "nu": 1993575}, {"pv": 26274120, "vv": 12717041, "pid": "749c360516ebe533", "uv": 234441, "pname": "百信手机网外链", "nu": 12717041}, {"pv": 6442292, "vv": 801841, "pid": "749c360516ebe533", "uv": 404290, "pname": "航海桌面导航", "nu": 801841}], "day": "15", "month": "12"}, {"year": "2011", "data": [{"pv": 8974934, "vv": 1135333, "pid": "749c360516ebe533", "uv": 384968, "pname": "mobile优酷友情合作", "nu": 1135333}, {"pv": 14935738, "vv": 1968406, "pid": "749c360516ebe533", "uv": 460287, "pname": "3G门户G浏览器", "nu": 1968406}, {"pv": 26481000, "vv": 12890380, "pid": "749c360516ebe533", "uv": 238530, "pname": "百信手机网外链", "nu": 12890380}, {"pv": 6517663, "vv": 807037, "pid": "749c360516ebe533", "uv": 411766, "pname": "航海桌面导航", "nu": 807037}], "day": "16", "month": "12"}, {"year": "2011", "data": [{"pv": 10026462, "vv": 1236730, "pid": "749c360516ebe533", "uv": 420494, "pname": "mobile优酷友情合作", "nu": 1236730}, {"pv": 17893920, "vv": 2373075, "pid": "749c360516ebe533", "uv": 533109, "pname": "3G门户G浏览器", "nu": 2373075}, {"pv": 29427041, "vv": 14269110, "pid": "749c360516ebe533", "uv": 276155, "pname": "百信手机网外链", "nu": 14269110}, {"pv": 7106385, "vv": 902434, "pid": "749c360516ebe533", "uv": 441599, "pname": "航海桌面导航", "nu": 902434}], "day": "17", "month": "12"}, {"year": "2011", "data": [{"pv": 10245069, "vv": 1289253, "pid": "749c360516ebe533", "uv": 428359, "pname": "mobile优酷友情合作", "nu": 1289253}, {"pv": 16366469, "vv": 2416836, "pid": "749c360516ebe533", "uv": 535023, "pname": "3G门户G浏览器", "nu": 2416836}, {"pv": 30343887, "vv": 14817690, "pid": "749c360516ebe533", "uv": 285791, "pname": "百信手机网外链", "nu": 14817690}, {"pv": 7162604, "vv": 903269, "pid": "749c360516ebe533", "uv": 451413, "pname": "航海桌面导航", "nu": 903269}], "day": "18", "month": "12"}, {"year": "2011", "data": [{"pv": 8928252, "vv": 1110394, "pid": "749c360516ebe533", "uv": 389020, "pname": "mobile优酷友情合作", "nu": 1110394}, {"pv": 12521422, "vv": 1880090, "pid": "749c360516ebe533", "uv": 448916, "pname": "3G门户G浏览器", "nu": 1880090}, {"pv": 26264948, "vv": 12763681, "pid": "749c360516ebe533", "uv": 248776, "pname": "百信手机网外链", "nu": 12763681}, {"pv": 6481151, "vv": 803660, "pid": "749c360516ebe533", "uv": 407620, "pname": "航海桌面导航", "nu": 803660}], "day": "19", "month": "12"}]}
        self.write({"status":"success", "items":os_data})
        self.finish()

class FactoryComp(BaseHandler):
    def get(self): 
        factory_json = clientcache.getFactoryName()
        
        _osid = self.get_argument('osid', '').strip()
        _period = self.get_argument('period', 'w').strip()
        
        _pd = 0
        if _period == 'm':
            _pd = 30
        elif _period == 'q':
            _pd = 90
        elif _period == 'y':
            _pd = 365
        else:
            _pd = 7
        
        if not _osid:
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        _time = datetime.datetime.now() + datetime.timedelta(days=0)
        st_time = (_time + datetime.timedelta(days=-_pd)).strftime( '%Y%m%d')
        et_time = _time.strftime( '%Y%m%d')

        #print 's:{0} , e:{1}'.format(st_time, et_time)
        
        all_data = []
        results = {}
        os_data = []

        db = connectMySQL.connMySQL_stat()
        c=db.cursor()
        sql = "SELECT f_id, sum(newuser)  FROM `stat_day_factory` WHERE date>= '{0}' and date < '{1}' and  os_id={2} and partner_mode=1 and pid_stat=1  group by f_id".format(st_time, et_time, _osid)
        logging.info(sql)
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        for x in res:
            factory = factory_json.get(x[0],'')
            tu = int(x[1])
            
            os_data.insert(0,{'factory':factory, 'tnu':tu})
        
        c.close()
        db.close()
        
        #print all_data
        #test = {"items": [{"year": "2011", "data": [{"pv": 8706151, "vv": 883726, "pid": "749c360516ebe533", "uv": 379018, "pname": "mobile优酷友情合作", "nu": 883726}, {"pv": 23036219, "vv": 2646085, "pid": "749c360516ebe533", "uv": 618283, "pname": "3G门户G浏览器", "nu": 2646085}, {"pv": 20426465, "vv": 10197784, "pid": "749c360516ebe533", "uv": 177077, "pname": "百信手机网外链", "nu": 10197784}, {"pv": 5984380, "vv": 675899.0, "pid": "749c360516ebe533", "uv": 380541, "pname": "航海桌面导航", "nu": 675899.0}], "day": "20", "month": "11"}, {"year": "2011", "data": [{"pv": 7738457, "vv": 788236, "pid": "749c360516ebe533", "uv": 344861, "pname": "mobile优酷友情合作", "nu": 788236}, {"pv": 19551225, "vv": 2101659, "pid": "749c360516ebe533", "uv": 510932, "pname": "3G门户G浏览器", "nu": 2101659}, {"pv": 18427618, "vv": 9257357, "pid": "749c360516ebe533", "uv": 157920, "pname": "百信手机网外链", "nu": 9257357}, {"pv": 5501749, "vv": 607562.0, "pid": "749c360516ebe533", "uv": 346352, "pname": "航海桌面导航", "nu": 607562.0}], "day": "21", "month": "11"}, {"year": "2011", "data": [{"pv": 7954177, "vv": 815959, "pid": "749c360516ebe533", "uv": 349300, "pname": "mobile优酷友情合作", "nu": 815959}, {"pv": 17299241, "vv": 2118076, "pid": "749c360516ebe533", "uv": 501143, "pname": "3G门户G浏览器", "nu": 2118076}, {"pv": 20366128, "vv": 10192610, "pid": "749c360516ebe533", "uv": 162041, "pname": "百信手机网外链", "nu": 10192610}, {"pv": 6219346, "vv": 899602, "pid": "749c360516ebe533", "uv": 363954, "pname": "航海桌面导航", "nu": 899602}], "day": "22", "month": "11"}, {"year": "2011", "data": [{"pv": 7977166, "vv": 842794, "pid": "749c360516ebe533", "uv": 348040, "pname": "mobile优酷友情合作", "nu": 842794}, {"pv": 16584716, "vv": 2097957, "pid": "749c360516ebe533", "uv": 490854, "pname": "3G门户G浏览器", "nu": 2097957}, {"pv": 20198366, "vv": 10391665, "pid": "749c360516ebe533", "uv": 161251, "pname": "百信手机网外链", "nu": 10391665}, {"pv": 5815811, "vv": 696805, "pid": "749c360516ebe533", "uv": 364588, "pname": "航海桌面导航", "nu": 696805}], "day": "23", "month": "11"}, {"year": "2011", "data": [{"pv": 7903879, "vv": 847485, "pid": "749c360516ebe533", "uv": 346739, "pname": "mobile优酷友情合作", "nu": 847485}, {"pv": 15089805, "vv": 2072196, "pid": "749c360516ebe533", "uv": 486514, "pname": "3G门户G浏览器", "nu": 2072196}, {"pv": 21005978, "vv": 10992027, "pid": "749c360516ebe533", "uv": 160747, "pname": "百信手机网外链", "nu": 10992027}, {"pv": 5907725, "vv": 714918, "pid": "749c360516ebe533", "uv": 365872, "pname": "航海桌面导航", "nu": 714918}], "day": "24", "month": "11"}, {"year": "2011", "data": [{"pv": 8036462, "vv": 859703, "pid": "749c360516ebe533", "uv": 351217, "pname": "mobile优酷友情合作", "nu": 859703}, {"pv": 16574256, "vv": 2147187, "pid": "749c360516ebe533", "uv": 508284, "pname": "3G门户G浏览器", "nu": 2147187}, {"pv": 20814242, "vv": 10802160, "pid": "749c360516ebe533", "uv": 166903, "pname": "百信手机网外链", "nu": 10802160}, {"pv": 5869896, "vv": 742478, "pid": "749c360516ebe533", "uv": 370612, "pname": "航海桌面导航", "nu": 742478}], "day": "25", "month": "11"}, {"year": "2011", "data": [{"pv": 9069765, "vv": 972571, "pid": "749c360516ebe533", "uv": 387447, "pname": "mobile优酷友情合作", "nu": 972571}, {"pv": 19110457, "vv": 2686480, "pid": "749c360516ebe533", "uv": 607180, "pname": "3G门户G浏览器", "nu": 2686480}, {"pv": 22990850, "vv": 12016920, "pid": "749c360516ebe533", "uv": 189509, "pname": "百信手机网外链", "nu": 12016920}, {"pv": 6409802, "vv": 819128, "pid": "749c360516ebe533", "uv": 408271, "pname": "航海桌面导航", "nu": 819128}], "day": "26", "month": "11"}, {"year": "2011", "data": [{"pv": 5164029, "vv": 905470, "pid": "749c360516ebe533", "uv": 402251, "pname": "mobile优酷友情合作", "nu": 905470}, {"pv": 22663416, "vv": 2959138, "pid": "749c360516ebe533", "uv": 625509, "pname": "3G门户G浏览器", "nu": 2959138}, {"pv": 22769949, "vv": 12005075, "pid": "749c360516ebe533", "uv": 194499, "pname": "百信手机网外链", "nu": 12005075}, {"pv": 6496387, "vv": 824110, "pid": "749c360516ebe533", "uv": 405406, "pname": "航海桌面导航", "nu": 824110}], "day": "27", "month": "11"}, {"year": "2011", "data": [{"pv": 7883670, "vv": 923891, "pid": "749c360516ebe533", "uv": 364508, "pname": "mobile优酷友情合作", "nu": 923891}, {"pv": 16813831, "vv": 2227148, "pid": "749c360516ebe533", "uv": 523621, "pname": "3G门户G浏览器", "nu": 2227148}, {"pv": 21876510, "vv": 11389919, "pid": "749c360516ebe533", "uv": 177008, "pname": "百信手机网外链", "nu": 11389919}, {"pv": 6182231, "vv": 786666, "pid": "749c360516ebe533", "uv": 377465, "pname": "航海桌面导航", "nu": 786666}], "day": "28", "month": "11"}, {"year": "2011", "data": [{"pv": 8932697, "vv": 979964, "pid": "749c360516ebe533", "uv": 371687, "pname": "mobile优酷友情合作", "nu": 979964}, {"pv": 18295828, "vv": 2271816, "pid": "749c360516ebe533", "uv": 529236, "pname": "3G门户G浏览器", "nu": 2271816}, {"pv": 24524788, "vv": 12383988, "pid": "749c360516ebe533", "uv": 179472, "pname": "百信手机网外链", "nu": 12383988}, {"pv": 7263753, "vv": 960556, "pid": "749c360516ebe533", "uv": 398384, "pname": "航海桌面导航", "nu": 960556}], "day": "29", "month": "11"}, {"year": "2011", "data": [{"pv": 9555887, "vv": 1046894, "pid": "749c360516ebe533", "uv": 383029, "pname": "mobile优酷友情合作", "nu": 1046894}, {"pv": 17147726, "vv": 2320942, "pid": "749c360516ebe533", "uv": 530421, "pname": "3G门户G浏览器", "nu": 2320942}, {"pv": 26100803, "vv": 13301933, "pid": "749c360516ebe533", "uv": 184759, "pname": "百信手机网外链", "nu": 13301933}, {"pv": 7837226, "vv": 1065946, "pid": "749c360516ebe533", "uv": 462942, "pname": "航海桌面导航", "nu": 1065946}], "day": "30", "month": "11"}, {"year": "2011", "data": [{"pv": 9191284, "vv": 997910, "pid": "749c360516ebe533", "uv": 378109, "pname": "mobile优酷友情合作", "nu": 997910}, {"pv": 17538103, "vv": 2212043, "pid": "749c360516ebe533", "uv": 508844, "pname": "3G门户G浏览器", "nu": 2212043}, {"pv": 23764115, "vv": 12167367, "pid": "749c360516ebe533", "uv": 182590, "pname": "百信手机网外链", "nu": 12167367}, {"pv": 7159327, "vv": 898155, "pid": "749c360516ebe533", "uv": 455937, "pname": "航海桌面导航", "nu": 898155}], "day": "01", "month": "12"}, {"year": "2011", "data": [{"pv": 8746707, "vv": 944695, "pid": "749c360516ebe533", "uv": 369130, "pname": "mobile优酷友情合作", "nu": 944695}, {"pv": 20920133, "vv": 2218533, "pid": "749c360516ebe533", "uv": 515960, "pname": "3G门户G浏览器", "nu": 2218533}, {"pv": 22158746, "vv": 11385687, "pid": "749c360516ebe533", "uv": 176859, "pname": "百信手机网外链", "nu": 11385687}, {"pv": 6573226, "vv": 820262, "pid": "749c360516ebe533", "uv": 429128, "pname": "航海桌面导航", "nu": 820262}], "day": "02", "month": "12"}, {"year": "2011", "data": [{"pv": 9729478, "vv": 1075297, "pid": "749c360516ebe533", "uv": 399538, "pname": "mobile优酷友情合作", "nu": 1075297}, {"pv": 25240817, "vv": 2707561, "pid": "749c360516ebe533", "uv": 601559, "pname": "3G门户G浏览器", "nu": 2707561}, {"pv": 23853896, "vv": 12166467, "pid": "749c360516ebe533", "uv": 197107, "pname": "百信手机网外链", "nu": 12166467}, {"pv": 6951907, "vv": 883009, "pid": "749c360516ebe533", "uv": 448783, "pname": "航海桌面导航", "nu": 883009}], "day": "03", "month": "12"}, {"year": "2011", "data": [{"pv": 6553890, "vv": 1000655, "pid": "749c360516ebe533", "uv": 410977, "pname": "mobile优酷友情合作", "nu": 1000655}, {"pv": 26534414, "vv": 2949384, "pid": "749c360516ebe533", "uv": 657332, "pname": "3G门户G浏览器", "nu": 2949384}, {"pv": 20255624, "vv": 9431825, "pid": "749c360516ebe533", "uv": 202836, "pname": "百信手机网外链", "nu": 9431825}, {"pv": 6194333, "vv": 709191, "pid": "749c360516ebe533", "uv": 401298, "pname": "航海桌面导航", "nu": 709191}], "day": "04", "month": "12"}, {"year": "2011", "data": [{"pv": 7504068, "vv": 931829, "pid": "749c360516ebe533", "uv": 372472, "pname": "mobile优酷友情合作", "nu": 931829}, {"pv": 26728075, "vv": 2215458, "pid": "749c360516ebe533", "uv": 556967, "pname": "3G门户G浏览器", "nu": 2215458}, {"pv": 22015006, "vv": 11073917, "pid": "749c360516ebe533", "uv": 181164, "pname": "百信手机网外链", "nu": 11073917}, {"pv": 6398145, "vv": 790298, "pid": "749c360516ebe533", "uv": 404533, "pname": "航海桌面导航", "nu": 790298}], "day": "05", "month": "12"}, {"year": "2011", "data": [{"pv": 9102575, "vv": 1062570, "pid": "749c360516ebe533", "uv": 375255, "pname": "mobile优酷友情合作", "nu": 1062570}, {"pv": 22657579, "vv": 2378515, "pid": "749c360516ebe533", "uv": 519891, "pname": "3G门户G浏览器", "nu": 2378515}, {"pv": 25411025, "vv": 12908338, "pid": "749c360516ebe533", "uv": 183900, "pname": "百信手机网外链", "nu": 12908338}, {"pv": 7267270, "vv": 921841, "pid": "749c360516ebe533", "uv": 431079, "pname": "航海桌面导航", "nu": 921841}], "day": "06", "month": "12"}, {"year": "2011", "data": [{"pv": 9323365, "vv": 1093411, "pid": "749c360516ebe533", "uv": 380987, "pname": "mobile优酷友情合作", "nu": 1093411}, {"pv": 27118625, "vv": 2324804, "pid": "749c360516ebe533", "uv": 509508, "pname": "3G门户G浏览器", "nu": 2324804}, {"pv": 27824591, "vv": 13416846, "pid": "749c360516ebe533", "uv": 201491, "pname": "百信手机网外链", "nu": 13416846}, {"pv": 8086168, "vv": 1000817, "pid": "749c360516ebe533", "uv": 442779, "pname": "航海桌面导航", "nu": 1000817}], "day": "07", "month": "12"}, {"year": "2011", "data": [{"pv": 9038836, "vv": 1063200, "pid": "749c360516ebe533", "uv": 373748, "pname": "mobile优酷友情合作", "nu": 1063200}, {"pv": 17233370, "vv": 2134431, "pid": "749c360516ebe533", "uv": 508371, "pname": "3G门户G浏览器", "nu": 2134431}, {"pv": 26251026, "vv": 12625185, "pid": "749c360516ebe533", "uv": 205565, "pname": "百信手机网外链", "nu": 12625185}, {"pv": 7313013, "vv": 902782, "pid": "749c360516ebe533", "uv": 411684, "pname": "航海桌面导航", "nu": 902782}], "day": "08", "month": "12"}, {"year": "2011", "data": [{"pv": 8363367, "vv": 1045071, "pid": "749c360516ebe533", "uv": 371220, "pname": "mobile优酷友情合作", "nu": 1045071}, {"pv": 17447010, "vv": 2132177, "pid": "749c360516ebe533", "uv": 481472, "pname": "3G门户G浏览器", "nu": 2132177}, {"pv": 23802977, "vv": 11606653, "pid": "749c360516ebe533", "uv": 208438, "pname": "百信手机网外链", "nu": 11606653}, {"pv": 6693872, "vv": 937565, "pid": "749c360516ebe533", "uv": 398948, "pname": "航海桌面导航", "nu": 937565}], "day": "09", "month": "12"}, {"year": "2011", "data": [{"pv": 9525712, "vv": 1127967, "pid": "749c360516ebe533", "uv": 399203, "pname": "mobile优酷友情合作", "nu": 1127967}, {"pv": 21035193, "vv": 2485316, "pid": "749c360516ebe533", "uv": 550803, "pname": "3G门户G浏览器", "nu": 2485316}, {"pv": 26775777, "vv": 12982224, "pid": "749c360516ebe533", "uv": 239143, "pname": "百信手机网外链", "nu": 12982224}, {"pv": 6780413, "vv": 835476, "pid": "749c360516ebe533", "uv": 422789, "pname": "航海桌面导航", "nu": 835476}], "day": "10", "month": "12"}, {"year": "2011", "data": [{"pv": 9700728, "vv": 1156906, "pid": "749c360516ebe533", "uv": 404359, "pname": "mobile优酷友情合作", "nu": 1156906}, {"pv": 18842952, "vv": 2470392, "pid": "749c360516ebe533", "uv": 546650, "pname": "3G门户G浏览器", "nu": 2470392}, {"pv": 27251966, "vv": 13014231, "pid": "749c360516ebe533", "uv": 249194, "pname": "百信手机网外链", "nu": 13014231}, {"pv": 6725342, "vv": 812185, "pid": "749c360516ebe533", "uv": 417197, "pname": "航海桌面导航", "nu": 812185}], "day": "11", "month": "12"}, {"year": "2011", "data": [{"pv": 8318720, "vv": 1010236, "pid": "749c360516ebe533", "uv": 361017, "pname": "mobile优酷友情合作", "nu": 1010236}, {"pv": 12905152, "vv": 1890186, "pid": "749c360516ebe533", "uv": 446430, "pname": "3G门户G浏览器", "nu": 1890186}, {"pv": 24365788, "vv": 11680267, "pid": "749c360516ebe533", "uv": 216079, "pname": "百信手机网外链", "nu": 11680267}, {"pv": 6134800, "vv": 730836, "pid": "749c360516ebe533", "uv": 388648, "pname": "航海桌面导航", "nu": 730836}], "day": "12", "month": "12"}, {"year": "2011", "data": [{"pv": 8689454, "vv": 1069416, "pid": "749c360516ebe533", "uv": 371320, "pname": "mobile优酷友情合作", "nu": 1069416}, {"pv": 14424782, "vv": 1908065, "pid": "749c360516ebe533", "uv": 451278, "pname": "3G门户G浏览器", "nu": 1908065}, {"pv": 25477197, "vv": 12223510, "pid": "749c360516ebe533", "uv": 223816, "pname": "百信手机网外链", "nu": 12223510}, {"pv": 6353900, "vv": 764547, "pid": "749c360516ebe533", "uv": 396732, "pname": "航海桌面导航", "nu": 764547}], "day": "13", "month": "12"}, {"year": "2011", "data": [{"pv": 8762564, "vv": 1100199, "pid": "749c360516ebe533", "uv": 375636, "pname": "mobile优酷友情合作", "nu": 1100199}, {"pv": 15790426, "vv": 1933915, "pid": "749c360516ebe533", "uv": 447268, "pname": "3G门户G浏览器", "nu": 1933915}, {"pv": 25910457, "vv": 12576917, "pid": "749c360516ebe533", "uv": 226918, "pname": "百信手机网外链", "nu": 12576917}, {"pv": 6392187, "vv": 788946, "pid": "749c360516ebe533", "uv": 395650, "pname": "航海桌面导航", "nu": 788946}], "day": "14", "month": "12"}, {"year": "2011", "data": [{"pv": 8648849, "vv": 1117817, "pid": "749c360516ebe533", "uv": 381679, "pname": "mobile优酷友情合作", "nu": 1117817}, {"pv": 14460326, "vv": 1993575, "pid": "749c360516ebe533", "uv": 458094, "pname": "3G门户G浏览器", "nu": 1993575}, {"pv": 26274120, "vv": 12717041, "pid": "749c360516ebe533", "uv": 234441, "pname": "百信手机网外链", "nu": 12717041}, {"pv": 6442292, "vv": 801841, "pid": "749c360516ebe533", "uv": 404290, "pname": "航海桌面导航", "nu": 801841}], "day": "15", "month": "12"}, {"year": "2011", "data": [{"pv": 8974934, "vv": 1135333, "pid": "749c360516ebe533", "uv": 384968, "pname": "mobile优酷友情合作", "nu": 1135333}, {"pv": 14935738, "vv": 1968406, "pid": "749c360516ebe533", "uv": 460287, "pname": "3G门户G浏览器", "nu": 1968406}, {"pv": 26481000, "vv": 12890380, "pid": "749c360516ebe533", "uv": 238530, "pname": "百信手机网外链", "nu": 12890380}, {"pv": 6517663, "vv": 807037, "pid": "749c360516ebe533", "uv": 411766, "pname": "航海桌面导航", "nu": 807037}], "day": "16", "month": "12"}, {"year": "2011", "data": [{"pv": 10026462, "vv": 1236730, "pid": "749c360516ebe533", "uv": 420494, "pname": "mobile优酷友情合作", "nu": 1236730}, {"pv": 17893920, "vv": 2373075, "pid": "749c360516ebe533", "uv": 533109, "pname": "3G门户G浏览器", "nu": 2373075}, {"pv": 29427041, "vv": 14269110, "pid": "749c360516ebe533", "uv": 276155, "pname": "百信手机网外链", "nu": 14269110}, {"pv": 7106385, "vv": 902434, "pid": "749c360516ebe533", "uv": 441599, "pname": "航海桌面导航", "nu": 902434}], "day": "17", "month": "12"}, {"year": "2011", "data": [{"pv": 10245069, "vv": 1289253, "pid": "749c360516ebe533", "uv": 428359, "pname": "mobile优酷友情合作", "nu": 1289253}, {"pv": 16366469, "vv": 2416836, "pid": "749c360516ebe533", "uv": 535023, "pname": "3G门户G浏览器", "nu": 2416836}, {"pv": 30343887, "vv": 14817690, "pid": "749c360516ebe533", "uv": 285791, "pname": "百信手机网外链", "nu": 14817690}, {"pv": 7162604, "vv": 903269, "pid": "749c360516ebe533", "uv": 451413, "pname": "航海桌面导航", "nu": 903269}], "day": "18", "month": "12"}, {"year": "2011", "data": [{"pv": 8928252, "vv": 1110394, "pid": "749c360516ebe533", "uv": 389020, "pname": "mobile优酷友情合作", "nu": 1110394}, {"pv": 12521422, "vv": 1880090, "pid": "749c360516ebe533", "uv": 448916, "pname": "3G门户G浏览器", "nu": 1880090}, {"pv": 26264948, "vv": 12763681, "pid": "749c360516ebe533", "uv": 248776, "pname": "百信手机网外链", "nu": 12763681}, {"pv": 6481151, "vv": 803660, "pid": "749c360516ebe533", "uv": 407620, "pname": "航海桌面导航", "nu": 803660}], "day": "19", "month": "12"}]}
        self.write({"status":"success", "items":os_data})
        self.finish()
